package com.bootdo.system.service.impl;

import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.CheckLogDao;
import com.bootdo.system.dao.SafeItemDao;
import com.bootdo.system.domain.CheckLogDO;
import com.bootdo.system.domain.ItemDO;
import com.bootdo.system.domain.SafeItemDO;
import com.bootdo.system.service.CheckLogService;
import com.bootdo.system.vo.CheckLogVO;
import com.bootdo.system.vo.SafeItemVO;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class CheckLogServiceImpl implements CheckLogService {

    @Autowired
    private CheckLogDao checkLogDao;

    @Autowired
    private FileService fileService;
    
    @Autowired
    private SafeItemDao safeItemDao;

    private static List<String> safePositionList;

    @Override
    public CheckLogVO get(String id) {
        CheckLogVO checkLogVO = checkLogDao.get(id);
        if(null != checkLogVO && StringUtils.isNotEmpty(checkLogVO.getAttachId())) {
            List<FileDO> filelist = fileService.queryListByAttachId(checkLogVO.getAttachId());
            checkLogVO.setFileList(filelist);
        }
        return checkLogVO;
    }

    @Override
    public List<CheckLogVO> list(Map<String, Object> map) {
        List<CheckLogVO> list = checkLogDao.list(map);
        if(!CollectionUtils.isEmpty(list)) {
            list.forEach(data ->{
                SafeItemDO safeItemDO = safeItemDao.get(data.getSafeId());
                if(null != safeItemDO) {
                    safePositionList = new ArrayList<String>();
                    safePositionList = getUpSafePositionList(safeItemDO.getParentId());
                    Collections.reverse(safePositionList);
                    if(!CollectionUtils.isEmpty(safePositionList)) {
                        String safePosition = "";
                        for(String data1 : safePositionList) {
                            safePosition += data1;
                        }
                        data.setSafePosition(safePosition);
                    }
                }
            });
        }
        return list;
    }

    private List<String> getUpSafePositionList(Long parentId) {
        SafeItemDO safeItemDO = safeItemDao.get(parentId);
        safePositionList.add(safeItemDO.getSafePosition());
        if(null != safeItemDO && safeItemDO.getParentId() != 0) {
            getUpSafePositionList(safeItemDO.getParentId());
        }
        return safePositionList;
    }

    @Override
    public int count(Map<String, Object> map) {
        return checkLogDao.count(map);
    }

    @Override
    public int save(CheckLogDO checkLog) {
        return checkLogDao.save(checkLog);
    }

    @Override
    public int update(CheckLogDO checkLog) {
        return checkLogDao.update(checkLog);
    }

    @Override
    public int remove(String id) {
        return checkLogDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        return checkLogDao.batchRemove(ids);
    }

}
